package com.woniuxy.mini;

import com.woniuxy.core.mapper.AllUserMapper;
import com.woniuxy.core.service.impl.UserSignServiceImpl;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;

/**
 * 用户签到控制器
 * 提供用户签到相关的 REST API 接口
 */
@RestController
@RequestMapping("userSign")
public class UserSignController {
    @Autowired
    private AllUserMapper allUserMapper;

    @Autowired
    private UserSignServiceImpl userSignService;

    /**
     * 用户签到接口（默认签到当天）
     * @param userId 用户ID
     * @return 签到结果（成功/已签到）
     */
    @PostMapping("/{userId}")
    public ResponseData<Boolean> sign(@PathVariable("userId") Long userId) {

        LocalDate date = LocalDate.now();
        boolean result = userSignService.sign(userId, date);
        return ResponseData.ok(result);
    }

    /**
     * 用户补签接口（指定日期签到）
     * @param userId 用户ID
     * @param date 补签日期（格式：yyyy-MM-dd）
     * @return 补签结果
     */
    @PostMapping("/{userId}/history")
    public ResponseData<Boolean> signHistory(
            @PathVariable("userId") Long userId,
            @RequestParam("date") String date) {
        LocalDate signDate = LocalDate.parse(date);
        boolean result = userSignService.sign(userId, signDate);
        return ResponseData.ok(result);
    }

    /**
     * 检查用户某天是否已签到
     * @param userId 用户ID
     * @param date 检查日期（格式：yyyy-MM-dd）
     * @return 是否已签到
     */
    @GetMapping("/{userId}/check")
    public ResponseData<Boolean> checkSigned(
            @PathVariable("userId") Long userId,
            @RequestParam("date") String date) {
        LocalDate checkDate = LocalDate.parse(date);
        boolean result = userSignService.checkSigned(userId, checkDate);
        return ResponseData.ok(result);
    }

    /**
     * 获取用户某月的签到状态列表
     * @param userId 用户ID
     * @param year 年份
     * @param month 月份
     * @return 签到状态列表（索引0对应1号，true=已签到）
     */
    @GetMapping("/{userId}/status")
    public ResponseData<List<Boolean>> getMonthSignStatus(
            @PathVariable("userId") Long userId,
            @RequestParam("year") int year,
            @RequestParam("month") int month) {
        LocalDate date = YearMonth.of(year, month).atDay(1);
        List<Boolean> statusList = userSignService.getMonthSignStatus(userId, date);
        return ResponseData.ok(statusList);
    }

    /**
     * 获取用户某月已签到的日期列表
     * @param userId 用户ID
     * @param year 年份
     * @param month 月份
     * @return 已签到的日期列表（如 [1,3,5]）
     */
    @GetMapping("/{userId}/days")
    public ResponseData<List<Integer>> getSignedDays(
            @PathVariable("userId") Long userId,
            @RequestParam("year") int year,
            @RequestParam("month") int month) {
        LocalDate date = YearMonth.of(year, month).atDay(1);
        List<Integer> signedDays = userSignService.getSignedDays(userId, date);
        return ResponseData.ok(signedDays);
    }

    /**
     * 获取用户连续签到天数
     * @param userId 用户ID
     * @return 连续签到天数
     */
    @GetMapping("/{userId}/continuous")
    public ResponseData<Integer> getContinuousSignDays(@PathVariable("userId") Long userId) {
        int days = userSignService.getContinuousSignDays(userId);
        return ResponseData.ok(days);
    }

    /**
     * 获取用户总签到次数
     * @param userId 用户ID
     * @param year 年份
     * @param month 月份
     * @return 总签到次数
     */
    @GetMapping("/{userId}/count")
    public ResponseData<Integer> getTotalSignCount(
            @PathVariable("userId") Long userId,
            @RequestParam("year") int year,
            @RequestParam("month") int month) {
        LocalDate date = YearMonth.of(year, month).atDay(1);
        int count = userSignService.getTotalSignCount(userId, date);
        return ResponseData.ok(count);
    }
}
